package com.capinfo.accounting.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.WebRequest;

import com.capinfo.accumulation.model.accounting.Kmszjbsj;
import com.capinfo.accumulation.model.accounting.Ztsz;
import com.capinfo.accumulation.model.pagemodel.Json;
import com.capinfo.accumulation.service.accounting.KmszService;
import com.capinfo.accumulation.service.accounting.ZtszService;
import com.capinfo.accumulation.service.accounting.mybits.KjkmqcszServiceI;
import com.capinfo.commons.project.model.security.SystemUser;
import com.capinfo.components.security.web.utils.WebRequestUtils;
import com.capinfo.framework.model.dictionary.Dictionary;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Controller
@RequestMapping("/business/accounting/kjkmqcsz")
public class KjkmqcszController{
	
	@Autowired
	private KjkmqcszServiceI kjkmqcszServiceI;
	
	@Autowired
	private KmszService kmszService;
	
	@Autowired
	private ZtszService ztszService;
	
	@RequestMapping("search.shtml")
	public String toIndex(HttpServletRequest request,HttpServletResponse response) throws IOException{  
		Set<Dictionary> kjndList = kmszService.getDictionariesBySortName("会计年度");
		request.setAttribute("kjndList", kjndList);
		List<Kmszjbsj> kmszList = kmszService.findMessage();
		request.setAttribute("kmszList", kmszList);
		return "business/accounting/kjkmqcsz/search";  
	}
	
	@RequestMapping(value="/list")      
	@ResponseBody	 
     public String select (HttpServletRequest request,HttpServletResponse response) throws IOException  {
		response.setHeader("Content-type", "textml;charset=UTF-8");
 		response.setCharacterEncoding("UTF-8");
 		String kjnd = request.getParameter("kjnd");
	    Map map = new HashMap();
	    map.put("currentPieceNum", 1);
	    map.put("perPieceSize", 10);
	    if(kjnd != null){
	    	map.put("KJND_EQ_NUM", kjnd);
	    }
	    List<Kmszjbsj> kjkmqcszList = kjkmqcszServiceI.selectBySfmj();
	  
	    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
	    gson.toJson(kjkmqcszList);
	    PrintWriter pw;
	    pw = response.getWriter();
	    pw.write(gson.toJson(kjkmqcszList));
        return null;
 	   }
	 
	@RequestMapping("form.shtml")
	public String toIndexs(HttpServletRequest request,HttpServletResponse response ,Long id) throws IOException{  
		
		Kmszjbsj kmszjbsj = kjkmqcszServiceI.selectByPrimaryKey(id+"");
		request.setAttribute("kmszjbsj", kmszjbsj);
		List<Kmszjbsj> kjkmqcszList = kjkmqcszServiceI.selectBySfmj();
		request.setAttribute("kjkmqcszList", kjkmqcszList);
		List<Ztsz> ztmcList = ztszService.getZtsz();
		request.setAttribute("ztmcList", ztmcList);
		Set<Dictionary> kjndList = kmszService.getDictionariesBySortName("会计年度");
		request.setAttribute("kjndList", kjndList);
		List<Kmszjbsj> yefxList = kjkmqcszServiceI.selectBySfmj();
		request.setAttribute("yefxList", yefxList);
		return "business/accounting/kjkmqcsz/form";
		  
	}
	
	@RequestMapping("/save_or_update.shtml")
	@ResponseBody
	public Json edit(Kmszjbsj kmszjbsj , HttpSession session,NativeWebRequest request) {
		
		Json j = new Json();
		try {
			
			kjkmqcszServiceI.updateByPrimaryKeySelective(kmszjbsj);
			j.setSuccess(true);
			j.setMsg("修改成功");
			j.setObj(kmszjbsj);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	@RequestMapping("/selectTiaojian.shtml")
	@ResponseBody
	public String selectByTiaojian(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setHeader("Content-type", "textml;charset=UTF-8");
 		response.setCharacterEncoding("UTF-8");
	    Map map = new HashMap();
	    map.put("currentPieceNum", 1);
	    map.put("perPieceSize", 10);
		List<Kmszjbsj> list = kjkmqcszServiceI.selectByTiaojian();
		request.setAttribute("list", list);
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		gson.toJson(list);
		PrintWriter pw;
		pw = response.getWriter();
		pw.write(gson.toJson(list));
		
		return null;
		
	}

}
